// pages/Readingdetails/Readingdetails.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示显示    0表示不显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '商品详情', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    login_token: '',
    order_id: '',
    goodinfo: {},
    swiperCurrent: 1, //轮播图下标
    pt: '',//1普通商品 
    ep: '',//2二配商品
    num: 1,
    zbgoodsType: ''//1直播商品
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    _this.setData({
      step: options.step,
      dzy: options.dzy,
      status: options.status,
      pt: options.pt,
      ep: options.ep,
      ptType: options.ptType,
      zbgoodsType: options.zbgoodsType

    })
    if (options.pt == 1 || options.ptType == 1) {
      $api.getproductsdetail({
        login_token: wx.getStorageSync('login_token'),
        good_id: options.id,
        is_live: options.zbgoodsType ? 1 : 0
      }).then((res) => {
        console.log(res.data.data)
        _this.setData({
          goodinfo: res.data.data,
          loadModal: false,
        })
        if (options.pt == 1 || options.ptType == 1) {
          let num = 1;
          let paymoney = 0;
          paymoney = res.data.data.price * num
          _this.setData({
            paymoney,
            num
          })
        }
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    } else {
      $api.getproductsdetail({
        login_token: wx.getStorageSync('login_token'),
        good_id: options.id,
        is_live: options.zbgoodsType ? 1 : 0
      }).then((res) => {
        console.log(res.data.data, 123)
        _this.setData({
          goodinfo: res.data.data,
          loadModal: false,
        })
        let num = 1;
        let paymoney = 0;
        paymoney = res.data.data.price * num
        _this.setData({
          paymoney,
          num
        })
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }
    
    //判断是否为iPhone X
    wx.getSystemInfo({
      success: res => {
        let modelmes = res.model;
        if (modelmes.search('iPhone X') != -1) {
          _this.setData({
            isIphoneX: true
          })
        }
      }
    })
  },
  onShow(){
    this.getcargoodnum()
  },
  //购物车列表
  getcargoodnum() {
    let _this = this
    $api.getcargoodnum({
      login_token: wx.getStorageSync('login_token'),
    }).then((res) => {
      if (res.data.code == 0) {
        _this.setData({
          cartgoods:res.data.data
        })
      }
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //轮播图下标
  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current + 1 //获取当前轮播图片的下标
    })
  },
  onReduce() {
    let _this = this;
    let paymoney = 0;
    let num = _this.data.num;
    let goodinfo = _this.data.goodinfo
    num--;
    num = num ? num : 1;
    paymoney = num * goodinfo.price
    _this.setData({ paymoney, num })
  },
  onAdd() {
    let _this = this;
    let paymoney = 0;
    let num = _this.data.num;
    let goodinfo = _this.data.goodinfo
    num++;
    paymoney = num * goodinfo.price
    _this.setData({ paymoney, num })
  },
  onpay() {
    let _this = this
    wx.removeStorageSync('address')
    wx.removeStorageSync('applyShop')
    wx.removeStorageSync('wait_pay')
    wx.removeStorageSync('giftId')
    wx.removeStorageSync('pay')
    wx.removeStorageSync('goodsList')
    wx.removeStorageSync('addressid')
    let goodinfo = _this.data.goodinfo
    goodinfo.num = 1
    let list = []
    list.push(goodinfo)
    list = JSON.stringify(list)
    if (_this.data.zbgoodsType) {
      wx.navigateTo({
        url: '/pages/reading/reading?state=3&&twoType=1&&zbgoodsType=1&&goods=' + list + '&is_register=' + goodinfo.is_register,
      })
    } else if (_this.data.ep) {
      wx.navigateTo({
        url: '/pages/reading/reading?state=3&&twoType=1&&goods=' + list + '&is_register=' + goodinfo.is_register,
      })
    } else {
      wx.navigateTo({
        url: '/pages/reading/reading?state=3&&twoType=1&&ptType=1&&goods=' + list + '&is_register=' + goodinfo.is_register,
      })
    }


  },
  // 加入购物车
  oncart(e) {
    let _this = this
    let goodinfo = _this.data.goodinfo
    let buy_goods = [{
      good_id: goodinfo.id,
      num: 1
    }]
    $api.addshoppingcart({
      buy_goods: JSON.stringify(buy_goods),
      mode: goodinfo.is_register,
      type: 1
    }).then((res) => {
      if(res.data.code ==0){
        wx.showToast({
          title: '已加入购物车',
          icon: 'none',
          duration: 2000
        })
        _this.getcargoodnum()
      }
      
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  // 跳转购物车
  gocart(){
    wx.navigateTo({
      url: '/pages/cart/cart'
    })
  }
})